<?php

class CategroyAction extends BaseAction
{
    /*
     * 活动栏目全部显示，不用分页
     */
    public function index()
    {
        $Categroy = D("Categroy");
        $cats = $Categroy->where($map)->order("orderid desc")->select();
        $cat_value = $this->_getCate();
        $CateCount = D("Categroy")->count();
        $CateOption = $this->_getCateOption();
        $this->assign("cateOption", $CateOption);
        $this->assign('catecount', $CateCount);
        $this->assign('categoty', $cat_value);
        $this->assign("title", '活动分类');
        $this->display("Public:categroy");
    }

    /*
     * 添加活动分类
     */
    public function add()
    {
        $new_cat = $this->_getCateOption();
        $this->assign("categoty", $new_cat);
        $this->display("Public:categroy");
    }


    public function doadd()
    {
        $Categroy = D("Categroy");
        $Categroy->Create();
        include COMMON_PATH.'zh2py.php';
        $data = array();
        $data['fatherid'] = $_POST['fatherid'];
        $posts = array_filter($_POST['name']);
        foreach ($posts as $name) {
            $data['name'] = $name;
            $data['pinyin'] = getPinyin($name);
            $Categroy->add($data);
        }
        $this->assign("jumpUrl", "__URL__");
        $this->success("添加成功！");
    }


    public function edit()
    {
        if ($_GET['id']) {
            $new_cat = $this->_getCateOption();
            $cat_data = D("Categroy")->getById($_GET['id']);
            $this->assign("cat_data", $cat_data);
            $this->assign("categoty", $new_cat);

            $this->display("Public:categroy");
        } else {
            $this->assign("jumpUrl", "__URL__");
            $this->error("数据不存在！");
        }
    }

    public function doedit()
    {
        $data = $_POST;
        if (D("Categroy")->save($data)) {
            $this->success("修改成功！");
        } else {
            $this->error("修改失败！");
        }
    }

    public function batch()
    {
        $act = $_REQUEST['act'];
        if($act == 'editeorder'){
            unset($_POST['act']);
            foreach ($_POST as $key => $value) {
                $Cate = D("Categroy");
                $data['id'] = $key;
                $data['orderid'] = $value;
                $Cate->save($data);
                F('cate',NULL);               
            }
             $this->success("修改成功！");
        }elseif($act == 'pinyin'){
            unset($_POST['act']);
            include COMMON_PATH.'zh2py.php';
            foreach ($_POST as $key => $value) {
                $Cate = D("Categroy");
                $data['id'] = $key;
                $pinyin = $Cate->getById($data['id']);
                $data['pinyin'] = getPinyin($pinyin['name']);
                $Cate->save($data);
                F('cate',NULL);
            }
             $this->success("生成拼音成功！");
        } else {
                 $this->_batch();
        }

    }

}

?>
